﻿using MainFrameDesign.ContentContorl;
using MainFrameDesign.Draws;
using MainZYPT;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using MainInterface;
using MainProGrid;
namespace MainFrameDesign.Component
{
    public class MyComponent : Div
    {
        public MyComponent() : base()
        {
            Padding padding = new Padding();
            padding.All = 0;
            this.Padding = padding;
            this.ContentHeight = 400;
            CustomProperty pro = new CustomProperty(this, typeof(int), "内容宽度", "ContentWidth", "外观", "设置组件的内容宽度，设置后组件将按照固定宽度展示，设置为0，则是在父容器中平铺");
            PMC.Properties.Add(pro);
            pro = new CustomProperty(this, typeof(int), "内容高度", "ContentHeight", "外观", "设置组件的内容高度，设置后组件将按照固定高度展示");
            PMC.Properties.Add(pro); 
        }
        public override object GetValue(string name)
        {
            object value = base.GetValue(name);
            if (value != null) 
            {
                return value;
            }
            if (name == "ContentWidth") 
            {
                return this.ContentWidth;
            }
            if (name == "ContentHeight") 
            {
                return this.ContentHeight;
            }
            return CurrentComponent.GetValue(name);
        }
        public override Boolean SetValue(string name, object value)
        {
            if (base.SetValue(name, value)) 
            {
                return true;
            }
            if (name == "ContentWidth")
            {
                this.ContentWidth=(int)value;
                return true;
            }
            if (name == "ContentHeight")
            {
                this.ContentHeight=(int)value;
                return true;
            }
            return  CurrentComponent.SetValue(name, value);
        }
        public MoudleCompoent CurrentComponent = new MoudleCompoent();
        public int ContentWidth { get; set; }
        public int ContentHeight { get; set; }
        public override Div AppendChild(Div div, bool isCallDraw)
        {
            return null;
        }

        public override int CallHeight()
        {
            if (this.ContentHeight == 0)
            {
                return base.CallHeight();
            }
            else
            {
                this.Height = this.Margin.Top + this.Margin.Bottom + this.Padding.Top + this.Padding.Bottom;
                this.Height += this.ContentHeight;
                return this.Height;
            }
        }

        public override void CallWidth()
        {
            if (this.ContentWidth == 0)
            {
                base.CallWidth();
            }
            else 
            {
                this.Width = this.ContentWidth;
                this.Width += this.Margin.Left + this.Margin.Right + this.Padding.Left + this.Padding.Right;
            }
        }

        public override void Draw(Graphics g)
        {
            if (IsVitrul)
            {
                g.DrawRectangle(MyDraw.VituralPen, this.Rect);
            }
            else
            {
                DrawDrawRect(g);
                DrawContent(g);
                DrawTitle(g);
                if (this.Root != null && this.Root.IsDebug)
                {
                    DrawDebug(g);
                }
            }
            foreach (var item in Childs)
            {
                item.Draw(g);
            }
        }

        public override void DrawContent(Graphics g)
        {
            g.FillRectangle(Brushes.White, ContentRect);
            if (this.CurrentComponent.Image != null)
            {
                g.DrawImage(this.CurrentComponent.Image, ContentRect);
            }
            else
            {
                g.DrawRectangle(MyDraw.BorderPen, ContentRect);
            }
        }

        public void LoadXml(System.Xml.XmlDocument doc)
        {
            CurrentComponent.LoadXmlZYK(doc);
            AddPro();
        }

        public Div Copy()
        {
            MyComponent myc = new MyComponent();
            myc.CurrentComponent = this.CurrentComponent.Copy();
            myc.AddPro();
            return myc;
        }

        public void AddPro() 
        {
            this.CurrentComponent.AddPro(this.PMC);
        }

        public override string GetDom(string dom)
        {
            string tem = "<div id='" + this.Name + "'>";
            tem += dom;
            tem += "</div>";
            return tem;
        }
        public override string GetCSS(string style)
        {
            string tem = "#" + this.Name + "{";
            tem += this.GetThisMargin();
            tem += this.GetThisPadding();
            tem += style;
            if (ContentWidth != 0) 
            {
                tem += "Width:"+ContentWidth+"px";
            }
            tem += "Height:"+ContentHeight+"px";
            tem += "}";
            return tem;
        }
        public override string GetScript(string script)
        {
            string tem = "var obj={};";
            tem += CurrentComponent.GetScript(string.Empty);
            tem += "$('#"+this.Name+"')."+CurrentComponent.TypeName+"(obj);";
            return tem;
        }
        public override string GetReferences(string reference)
        {
            return CurrentComponent.Reference;
        }
        public override string GetLinks(string links)
        {
            return CurrentComponent.Links;
        }
        public override void GetFiles(List<string> list)
        {
            CurrentComponent.GetFiles(list);
        }
    }
}
